#!/usr/bin/perl -w
my $copyright = 'Copyright (C) 2007,2009,2012,2014,2015,2016,2017,2018 Olly Betts';
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

use strict;

my $generated_warning =
"/* Warning: This file is generated by $0 - do not modify directly! */\n";

my %uncopyableclasses = (
    'BB2Weight' => '',
    'BM25Weight' => '',
    'BoolWeight' => '',
    'DLHWeight' => '',
    'IfB2Weight' => '',
    'IneB2Weight' => '',
    'InL2Weight' => '',
    'TfIdfWeight' => '',
    'TradWeight' => '',
    'ValuePostingSource' => '0',
    'ValueWeightPostingSource' => '0',
    'DecreasingValueWeightPostingSource' => '0',
    'ValueCountMatchSpy' => '0',
    'ValueMapPostingSource' => '0',
    'FixedWeightPostingSource' => '0',
    'DateRangeProcessor' => '0',
    'MultiValueKeyMaker' => '',
    'NumberRangeProcessor' => '0, ""',
    'RangeProcessor' => '',
    'SimpleStopper' => '',
);

my %copyableclasses = (
    'Database' => '',
    'Document' => '',
    'Diversify' => '10, 4',
    'ESet' => '',
    'ESetIterator' => '',
    'Enquire' => 'Xapian::Database(std::string(), Xapian::DB_BACKEND_INMEMORY)',
    'MSet' => '',
    'MSetIterator' => '',
    'PositionIterator' => '',
    'PostingIterator' => '',
    'Query' => '',
    'QueryParser' => '',
    'Registry' => '',
    'RSet' => '',
    'Stem' => '',
    'TermGenerator' => '',
    'TermIterator' => '',
    'ValueIterator' => '',
    'WritableDatabase' => ''
);

my %no_get_description = (
    'DateRangeProcessor' => 1,
    'MultiValueKeyMaker' => 1,
    'NumberRangeProcessor' => 1,
    'RangeProcessor' => 1,
    'Registry' => 1,
    'BB2Weight' => 1,
    'BM25Weight' => 1,
    'BoolWeight' => 1,
    'DLHWeight' => 1,
    'IfB2Weight' => 1,
    'IneB2Weight' => 1,
    'InL2Weight' => 1,
    'TfIdfWeight' => 1,
    'TradWeight' => 1
);

print <<'END';
/** @file
 * @brief test common features of API classes
 */
END
print $generated_warning;
print <<"END";
/* $copyright
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <config.h>

#include "api_generated.h"

#define XAPIAN_DEPRECATED(D) D
#include <xapian.h>

#include "apitest.h"
#include "testutils.h"

using namespace std;

/// Check uncopyable API classes which should have a default ctor actually do.
DEFINE_TESTCASE(defaultctor1, !backend) {
END

for my $class (sort keys %uncopyableclasses) {
    my $params = $uncopyableclasses{$class};
    if ($params ne '') {
	$params = "($params)";
    }
    my $get_description = ! exists $no_get_description{$class};
    my $object = lc $class;
    my $full_class = "Xapian::$class";
    print "    $full_class $object$params;\n";
    if ($get_description) {
	print "    {\n";
	print "\tconst string& desc = $object.get_description();\n";
	print "\ttout.clear();\n";
	print "\ttout << desc << endl;\n";
	print "\tTEST(startswith(desc, \"$class(\") ||\n";
	print "\t     startswith(desc, \"$full_class(\"));\n";
	print "\tTEST(desc.back() == ')');\n";
	print "    }\n";
    }
}

print <<'END';
}

/// Test that API classes have a copy ctor and assignment operator.
DEFINE_TESTCASE(copyassign1, !backend) {
END

for my $class (sort keys %copyableclasses) {
    my $params = $copyableclasses{$class};
    if ($params =~ /INMEMORY/) {
	print "#ifdef XAPIAN_HAS_INMEMORY_BACKEND\n";
    }
    if ($params ne '') {
	$params = "($params)";
    }
    my $get_description = ! exists $no_get_description{$class};
    my $object = lc $class;
    $class = "Xapian::$class";
    print "    $class $object$params;\n";
    print "    TEST(!$object.get_description().empty());\n" if $get_description;
    print "    $class copy_$object($object);\n";
    print "    TEST(!copy_$object.get_description().empty());\n" if $get_description;
    print "    $class move_$object(std::move($object));\n";
    print "    TEST(!move_$object.get_description().empty());\n" if $get_description;
    print "    $object = copy_$object;\n";
    print "    TEST(!$object.get_description().empty());\n" if $get_description;
    print "    copy_$object = std::move(move_$object);\n";
    print "    TEST(!copy_$object.get_description().empty());\n" if $get_description;
    if ($params =~ /INMEMORY/) {
	print "#endif\n";
    }
    print "\n";
}

print <<'END';
}
END
